package com.chen.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * 
 * <p>Title:SendMessgeUtil</>
 * <p>Description:</p>
 * @author chen
 * 2017年10月19日下午4:09:48
 */
public class XmlMessgeUtil {
	//MESSAGE><MCODE>101440</MCODE><MID>R1710121266083</MID><DATE>20480605</DATE><TIME>175029</TIME><PAYDAY>20480605</PAYDAY><MERID>888001830000024</MERID><ORDERDATE>20171012</ORDERDATE><ORDERID>201710120002</ORDERID><STATUS>SUCCESS</STATUS><REMARK></REMARK><FROZENNO>111111</FROZENNO><SIGN>RrEb1YXZaF+AKJ36sJIUdUnM54YV6BgDStdI3WGiAfI8Av18GZBp2YSLVIDaxyDRq+/g4Y8W+U03WYAoC6n2cXpDWGMRfV1nE+R0sLDFhdTJZI4lLL8iz690UiIreZQNZWFVIvVYmnQ+azJnUinpmJvnN1wDbHF/tJOoqtkQe14=</SIGN></MESSAGE>
	public static Map<String, String> requestXml2Map(String dataXML) throws Exception {
		Map<String, String> dataMap = new HashMap<>(30);
		StringBuffer signStr = new StringBuffer();
		try {
		    ByteArrayInputStream bais = new ByteArrayInputStream(dataXML.getBytes("utf-8"));
		    Document doc =  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(bais));
			doc.normalize();
			Element root = (Element) doc.getElementsByTagName("MESSAGE").item(0);
			NodeList nodeList = root.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				String key = nodeList.item(i).getNodeName();
				Node child = nodeList.item(i).getFirstChild();
				String value = "";
				if (child != null) {
					value = child.getNodeValue();
					dataMap.put(key, value);
				}

				if (!"SIGN".equals(key)) {
						signStr.append(key).append("=").append(value).append("&");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("requetXml2Map异常！！！");
		}
		String signString = signStr.toString();
		int signStringLen = signString.length();
		signString = signString.substring(0, signStringLen - 1);
		dataMap.put("SIGNSTR", signString);
		return dataMap;
	}
	
	public static Map<String, String> xml2Map(String dataXML) throws Exception {
		Map<String, String> dataMap = new HashMap<>(30);
		StringBuffer signStr = new StringBuffer();
		try {
		    ByteArrayInputStream bais = new ByteArrayInputStream(dataXML.getBytes("utf-8"));
		    Document doc =  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(bais));
			doc.normalize();
			Element root = (Element) doc.getElementsByTagName("MESSAGE").item(0);
			NodeList nodeList = root.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				String key = nodeList.item(i).getNodeName();
				Node child = nodeList.item(i).getFirstChild();
				String value = "";
				if (child != null) {
					value = child.getNodeValue();
					dataMap.put(key, value);
				}

				if (!"SIGN".equals(key)) {
						signStr.append(value).append("|");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("支付请求时，解包异常！！！");
		}
		String signString = signStr.toString();
		int signStringLen = signString.length();
		signString = signString.substring(0, signStringLen - 1);
		dataMap.put("SIGNSTR", signString.replace("<", "&lt;").replace(">", "&gt;"));
		return dataMap;
	}
}
